import * as fs from 'fs';
import * as path from 'path'; // path: 用于获取路径

export function getMysqlUsernameAndPassword() {
  // const homeDir = os.homedir(); // C:\Users\zhang

  // const usernamePath = path.resolve(homeDir, '.vben', 'username') // C:\Users\zhang\.vben\username
  // const passwordPath = path.resolve(homeDir, '.vben', 'password') // C:\Users\zhang\.vben\password

  const usernamePath = path.resolve(__dirname, '../..', '.vben', 'password'); // E:\code\study\vue_login_book\nestjs-book-admin\.vben\password
  const passwordPath = path.resolve(__dirname, '../..', '.vben', 'password'); // E:\code\study\vue_login_book\nestjs-book-admin\.vben\password

  const username = fs.readFileSync(usernamePath).toString().trim(); // 读取文件内容
  const password = fs.readFileSync(passwordPath).toString().trim(); // 读取文件内容

  return {
    username,
    password,
  };
}

/**
 * 请求成功时候的返回数据格式统一处理
 * @param data
 * @param mag
 * @returns
 */
export function success(data, msg) {
  return {
    code: 0,
    result: data,
    message: msg,
  };
}

/**
 * 请求失败时候的返回数据格式统一处理
 * @param msg
 * @returns
 */
export function error(msg) {
  return {
    code: -1,
    message: msg,
  };
}

/**
 * 对返回的Promise进行处理
 */
export function wrapperResponse(promise, msg) {
  return promise
    .then((data) => success(data, msg))
    .catch((err) => error(err.message));
}
